package com.yang.jpa.service;

import com.yang.jpa.dao.*;
import com.yang.jpa.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImp implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private MealDao mealDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProjectTypeDao projectTypeDao;

    @Autowired
    private ProductTypeDao productTypeDao;

    @Autowired
    private PlayerDao playerDao;

    @Override
    public List<Member> queryAll() {
//        Member m = memberDao.findById(2).get();
//        memberDao.delete(m);

        //update
//        Member m = new Member();
//        m.setId(1);
//        memberDao.save(m);

        List<Member> mList = memberDao.findAll();

        List<Member> mListf = memberDao.findByName("M1");
        System.out.println("mListf："+mListf);

        List<Member> mList1 = memberDao.findMemberListByName("M1");
        System.out.println("mList1："+mList1);

        List<Member> mList2 = memberDao.findMemberListByNameCreatedAt("M2", "2020-10-03%");
        System.out.println("mList2："+mList2);

        List<Member> mList3 = memberDao.findMemberListByNameCreatedAtParam("M%", "2020-10-03%");
        System.out.println("mList3："+mList3);

//        memberDao.updateMemberById("m3", 2);

        Specification<Member> memberSpecification = new Specification<Member>() {
            @Override
            public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> name = root.get("name");
                Path<Object> id = root.get("id");
                Predicate predicateName = criteriaBuilder.equal(name, "M1");
                Predicate predicateId = criteriaBuilder.equal(id, 1);

                return criteriaBuilder.and(predicateName, predicateId);
            }
        };
        System.out.println("memberSpecificationList："+memberDao.findAll(memberSpecification));

        return mList;
    }

    @Override
    public List<Player> dynamicQuery(PutPlayer player, Integer page, Integer size){

        Sort.Direction sort =  Sort.Direction.DESC;
        Pageable pageable = PageRequest.of(page-1, size, sort, "number");

        Specification<Player> query = new Specification<Player>() {
            @Override
            public Predicate toPredicate(Root<Player> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!player.getNumber().equals("")){
                    predicates.add(criteriaBuilder.equal(root.get("number"),player.getNumber()));
                }
                if(!player.getGender().equals("")){
                    predicates.add(criteriaBuilder.equal(root.get("gender"),player.getGender()));
                }
                if(!player.getName().equals("")){
                    predicates.add(criteriaBuilder.like(root.get("name"),"%"+player.getName()+"%"));
                }
                if(!player.getIdCard().equals("")){
                    predicates.add(criteriaBuilder.like(root.get("idCard"),"%"+player.getIdCard()+"%"));
                }
                if(!player.getWorksName().equals("")){
                    predicates.add(criteriaBuilder.like(root.get("worksName"),"%"+player.getWorksName()+"%"));
                }
                if(!player.getWorksType().equals("")){
                    predicates.add(criteriaBuilder.equal(root.get("worksType"),player.getWorksType()));
                }
                if(!player.getSchool().equals("")){
                    predicates.add(criteriaBuilder.equal(root.get("school"),player.getSchool()));
                }
                if(!player.getSpeciality().equals("")){
                    predicates.add(criteriaBuilder.equal(root.get("speciality"),player.getSpeciality()));
                }
                if(!player.getWorksTime().equals("")){
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("worksTime").as(String.class),player.getWorksTime()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        //不分页
//        return playerDao.findAll(query);

        //分页
        return playerDao.findAll(query, pageable).getContent();

//        Pageable page1 = PageRequest.of(0, 2);
//        System.out.println("total："+page1.getOffset());
//
//        Sort sort1 = Sort.by(Sort.Direction.ASC, "number");
//        return playerDao.findAll(query, sort1);
    }

    @Override
    @Transactional
    public Map<String, Object> insertAll(List<Member> mList) {
        for(Member m : mList){
            for (Meal e:m.getMeallist()){

                for(Project j : e.getProjectList()){
                    projectTypeDao.save(j.getType());
                    //projectDao.save(j);
                }
                projectDao.saveAll(e.getProjectList());

                for(Product d : e.getProductList()){
                    productTypeDao.save(d.getType());
                    //productDao.save(d);
                }
                productDao.saveAll(e.getProductList());
                //mealDao.save(e);
            }
            mealDao.saveAll(m.getMeallist());
            //memberDao.save(m);
        }
        memberDao.saveAll(mList);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    @Override
    public Map<String, Object> jpaTest(){

        ProductType dt1 = new ProductType(1, "DT1");
        ProductType dt2 = new ProductType(2, "DT2");
        productTypeDao.save(dt1);
        productTypeDao.save(dt2);

        ProjectType jt1 = new ProjectType(1, "JT1");
        ProjectType jt2 = new ProjectType(2, "JT2");
        projectTypeDao.save(jt1);
        projectTypeDao.save(jt2);

        Product d1 = new Product(1,"D1");
        Product d2 = new Product(2,"D2");
        d1.setType(dt2);
        d2.setType(dt1);
        productDao.save(d1);
        productDao.save(d2);

        Project j1 = new Project(1,"J1");
        Project j2 = new Project(2,"J2");
        j1.setType(jt2);
        j2.setType(jt1);
        projectDao.save(j1);
        projectDao.save(j2);

        Meal e1 = new Meal(1,"E1");
        Meal e2 = new Meal(2,"E2");
        Meal e3 = new Meal(3,"E3");
        e1.getProductList().add(d2);
        e2.getProductList().add(d1);
        e1.getProjectList().add(j2);
        e2.getProjectList().add(j1);
        e3.getProductList().add(d1);
        e3.getProjectList().add(j1);
        mealDao.save(e1);
        mealDao.save(e2);
        mealDao.save(e3);

        Member m1 = new Member(1,"M1");
        Member m2 = new Member(2,"M2");
        m1.getMeallist().add(e1);
        m1.getMeallist().add(e2);
        m2.getMeallist().add(e3);
        memberDao.save(m1);
        memberDao.save(m2);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

}
